package com.uinnova.springboot.demo.mybatis;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 张攀钦
 * @date 2018-12-31-0:38
 */
public class SexEnumTypeHandleer implements TypeHandler<SexEnum> {
    private final Map<String, SexEnum> sexEnumMap = new HashMap<>(16);

    public SexEnumTypeHandleer() {

    }

    {
        for (SexEnum sexEnum : SexEnum.values()) {
            sexEnumMap.put(sexEnum.getValue(), sexEnum);
        }
    }

    @Override
    public void setParameter(PreparedStatement ps, int i, SexEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getValue());
    }

    @Override
    public SexEnum getResult(ResultSet rs, String columnName) throws SQLException {
        String string = rs.getString(columnName);
        return sexEnumMap.get(string);
    }

    @Override
    public SexEnum getResult(ResultSet rs, int columnIndex) throws SQLException {
        String string = rs.getString(columnIndex);
        return sexEnumMap.get(string);
    }

    @Override
    public SexEnum getResult(CallableStatement cs, int columnIndex) throws SQLException {
        String string = cs.getString(columnIndex);
        return sexEnumMap.get(string);
    }
}
